<?php

namespace api\store;

class Store
{
    const store_name = 'store';

    private $_name = '';
    private $_result = [];

    public function __construct($store_name)
    {
        $this->_name = $store_name;
    }

    private function store_get($name, $default = false)
    {
        if (config('app_store'))
            return StoreRemote::data_get($name, $default);
        else
            return StoreLocal::data_get($name, $default);
    }

    private function store_set($name, $value)
    {
        if (config('app_store'))
            return StoreRemote::data_set($name, $value);
        else
            return StoreLocal::data_set($name, $value);
    }

    private function get_new_id()
    {
        $store = $this->store_get(self::store_name);
        if (empty($store)) $store = [];
        if (empty($store['ids'][$this->_name])) $store['ids'][$this->_name] = 0;

        $id = intval($store['ids'][$this->_name]) + 1;
        $store['ids'][$this->_name] = $id;
        $this->store_set(self::store_name, $store);
        return $id;
    }

    private function reset_id()
    {
        $store = $this->store_get(self::store_name);
        if (empty($store)) $store = [];
        $store['ids'][$this->_name] = 0;
        $this->store_set(self::store_name, $store);
        return true;
    }

    private function get_data()
    {
        $data = $this->store_get($this->_name);
        if (empty($data))
            $data = [];
        return $data;
    }

    public function page($page_current = null, $item_count = 10)
    {
        $data = $this->_result;

        $item_total = count($data);
        $page_total = ceil($item_total / intval($item_count));

        $page_current = intval($page_current);
        $page_current = $page_current < 1 ? 1 : $page_current;
        $page_current = $page_current > $page_total ? $page_total : $page_current;

        $page_pre = $page_current - 1;
        $page_pre = $page_pre < 1 ? null : $page_pre;
        $page_next = $page_current + 1;
        $page_next = $page_next > $page_total ? null : $page_next;

        $start = intval(($page_current - 1) * $item_count);

        $list = array_slice($data, $start, $item_count);
        $page = ['item_total' => $item_total, 'page_total' => $page_total, 'page_current' => $page_current, 'page_pre' => $page_pre, 'page_next' => $page_next];

        $result = ['list' => $list, 'page' => $page];
        return $result;
    }

    public function data()
    {
        $data = $this->_result;
        return $data;
    }

    public function limit($start, $length)
    {
        $data = $this->_result;
        $data = array_slice($data, $start, $length);
        return $data;
    }

    public function add($item)
    {
        if (!is_array($item))
            return false;
        $item['id'] = $this->get_new_id();
        $data = $this->get_data();
        array_unshift($data, $item);


        $result = $this->store_set($this->_name, $data);
        return $result;
    }

    public function get($value, $key = 'id')
    {
        $data = $this->get_data();
        foreach ($data as $row) {
            if (array_key_exists($key, $row)) {
                if (!empty($key) && $key != '*') {
                    if ($row[$key] == $value)
                        return $row;
                } else if (array_search($value, $row))
                    return $row;
            }
        }
        return false;
    }

    public function select($value = null, $key = 'id', $fuzzy = false)
    {
        $data = $this->get_data();
        if (empty($value)) {
            $this->_result = $data;
            return $this;
        }

        $new_data = [];
        foreach ($data as $row) {
            if (array_key_exists($key, $row)) {
                if ($fuzzy && stristr($row[$key], $value))
                    array_push($new_data, $row);
                elseif ($row[$key] == $value)
                    array_push($new_data, $row);
            }
        }
        $this->_result = $new_data;
        return $this;
    }

    public function del($value, $key = 'id')
    {
        $data = $this->get_data();
        $new_data = [];
        foreach ($data as $row) {
            if (array_key_exists($key, $row)) {
                $v = $row[$key];
                if (is_array($value)) {
                    if (!in_array($v, $value))
                        array_push($new_data, $row);
                } else {
                    if ($v != $value)
                        array_push($new_data, $row);
                }
            }
        }
        $this->store_set($this->_name, $new_data);
        $del = count($data) - count($new_data);
        return $del;
    }

    public function edit($item)
    {
        $key = 'id';
        $data = $this->get_data();
        if (is_array($item) && array_key_exists($key, $item))
            $id = $item[$key];
        else
            return false;
        $new_data = [];
        foreach ($data as $row) {
            if (array_key_exists($key, $row)) {
                if ($row[$key] == $id)
                    array_push($new_data, array_merge($row, $item));
                else
                    array_push($new_data, $row);
            }
        }
        $this->store_set($this->_name, $new_data);
        return true;
    }

    public function clear()
    {
        $this->reset_id();
        $this->store_set($this->_name, []);
        return true;
    }
}